package fr.uvsq.isty.bdh.hchess.gui.menu;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import fr.uvsq.isty.bdh.hchess.gui.MainWindow;
import fr.uvsq.isty.bdh.hchess.ranking.Player;
import fr.uvsq.isty.bdh.hchess.ranking.Players;

public class EloMenu extends JPanel {

	/**
	 * The parent windows
	 */
	private MainWindow MW;
	
	/**
	 * The Background picture
	 */
	private Image bg;
	
	/**
	 * The button to go back to the menu.
	 */
	private JButton back;
	
	/**
	 * Area where the player are printed
	 */
	private JTextArea listArea;
	
	/**
	 * Bottom panel for back button
	 */
	private JPanel bottom;
	
	/**
	 * Constructor, where all the job is done...
	 * @param parent Parent window
	 */
	public EloMenu(MainWindow parent) {
		this.MW = parent;
		
		//Background
		try {
			bg = ImageIO.read(getClass().getClassLoader()
                                .getResource("menu/menu.jpg"));
		} catch (IOException e1) {			
			e1.printStackTrace();
		}
		
		//Main area in the middle
		listArea = new JTextArea();
		listArea.setPreferredSize(new Dimension(200, 400));
		listArea.setEditable(false);
		
		try {
			for( Player P: Players.getInstance().getItemsWithFilter("") )
			{
				listArea.append(P.getName()+" "+P.getRating());
				listArea.append("\n");
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		JEmptyPanel center = new JEmptyPanel();
		center.add(listArea, BorderLayout.CENTER);
		
		center.setAlignmentX(Component.CENTER_ALIGNMENT);
		this.add(Box.createVerticalGlue());
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));		
		this.add(center);
		
		
		//Bottom buttons (back & start)
		bottom = new JEmptyPanel();
		
		//Button Back
		back = new JButton("Retour");		
			
		bottom.add(back, BorderLayout.PAGE_START);
		
		back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new MainMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });		
	
		
		//Bottom area
		//this.add(bottom, BorderLayout.PAGE_END);
		bottom.setAlignmentX(Component.CENTER_ALIGNMENT);		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(Box.createVerticalGlue());
		this.add(bottom);
		
	}
	
	/**
	 * Just to print the background
	 */
	 @Override
	 public void paintComponent(Graphics g){
		g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
	 }

}
